package com.pur.zsms.company.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.pur.zsms.common.dao.GenericDao;
import com.pur.zsms.common.dao.JpaTemplate;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.company.dao.ICompanyDao;
import com.pur.zsms.company.po.CompanyPo;
import com.pur.zsms.company.po.CompanySupPo;
import com.pur.zsms.company.vo.CompanyVo;

@Repository("companyDao")
public class CompanyDaoImpl extends GenericDao implements ICompanyDao {

	public PaginationSupport listCompany(PageController pageController, CompanyVo companyVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.company_id,a.company_name,a.shop_flag,a.sup_flag,a.dis_flag from t_company a ");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport listCompanyForSup(PageController pageController, CompanyVo companyVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.company_id,a.company_name,a.shop_flag,a.sup_flag,a.dis_flag from t_company a where 1=1");
		ql.append(" AND exists ( select 1 from t_company_sup b WHERE b.sup_id = '" + companyVo.getSupId() + "' AND b.company_id = a.company_id)");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public List<CompanyPo> getCompanyPo(CompanyVo c) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o from CompanyPo o  where o.companyName = '" + c.getCompanyName() + "'");
		List<CompanyPo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<CompanyPo> getCompanyPo2(CompanyVo c) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o from CompanyPo o  where 1=1 ");
		if (c.getCompanyId() != 0) {
			ql.append(" AND o.companyId = '" + c.getCompanyId() + "' ");

		}
		List<CompanyPo> list = template.find(ql.toString(), null);
		return list;
	}

	public void addCompanyPo(CompanyPo c) throws GenericException {
		this.persist(c);
	}

	public void addCompanySupPo(CompanySupPo c) throws GenericException {
		this.persist(c);
	}

}
